<?php
class Menu_item_manager extends Abstract_manager
{
    
    function __construct()
    {
        parent::__construct();
        $this->table = "menu_items";
    }
    public function createNewMenuItem()
    {
    	
    }
	public function getMenuType($menu_item_id)
   	{
   		return $this->db->query("SELECT * FROM menu_types WHERE id IN(SELECT menu_type_id FROM $this->table WHERE id = '$menu_item_id')")->row();
   	}
    public function getLinkedItem($menu_item_id)
    {
    	$alias  = $this->db->get_where($this->table, array("id" => $menu_item_id))->row()->alias;
    	
    	$from_article = $this->db->get_where("articles", array('alias' => $alias));
    	$from_category = $this->db->get_where("categories", array('alias' => $alias));
    	
    	if($from_article->num_rows() > 0)
    	{
    		return array("type" => "article", "article_id" => $from_article->row()->id );
    	}
    	if($from_category->num_rows() > 0)
    	{
    		return array("type" => "category", "category_id" => $from_category->row()->id );
    	}
    }
 
	public function readMenuItemsList($menu_id)
	{
		return $this->db->get_where($this->table, array("menu_id" => $menu_id))->result();
	}
	public function updateItem($menu_item_id, $alias = false)
	{
		$menu_type = $this->getMenuType($menu_item_id);
		
		if($menu_type->title == "article")
		{
			$this->post_data['menu_type_id'] = 1;
			$this->db->where('id', $menu_item_id);
			$this->db->update($this->table, $this->post_data);
		}
		if($menu_type->title == "category")
		{
			$this->post_data['menu_type_id'] = 2;
			$this->db->where('id', $menu_item_id);
			$this->db->update($this->table, $this->post_data);
		}
	}
}